<?php
namespace app\api\controller;

use app\api\model\SortModel;

 use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
class Manager extends Common
{


    public function index()
    {
        $db=Db::name("manager");
        $query = input('get.query');//关键字
        $pagenum = input('get.pagenum');//当前页码
        $pagesize = input('get.pagesize');//当前显示行数

        // $w['m.status'] = array('eq',1);
        if($query) $w['m.username'] = array('like','%'.$query.'%');
        $list = Db::name('manager')->alias('m')
        ->join('role r','r.id=m.role_id')
        ->field('m.id,m.username,m.realname,r.role_name,m.status,m.password,m.role_id')
        ->where($w)->limit(($pagenum - 1) * $pagesize,$pagesize)->select();
        foreach ($list as $k => $v) {
            $list[$k]['status'] = $v['status']==1?true:false;
        }
        // 总页数/行数
        $count = Db::name('manager')->alias('m')
        ->join('role r','r.id=m.role_id')->where($w)->count();
        // 总页数/行数
        // $total = round($count/$pagesize);
        $total = $count;
        // dump($list);die();
        $arr['data']['pagenum']=$pagenum;
        $arr['data']['total']=$total;
        $arr['data']['users']=$list;
        $arr['meta']=array('msg'=>'获取用户列表成功','status'=>200);
        return json_encode($arr);
    }

    // 获取管理角色数据
    public function options()
    {
        $db=Db::name("role");
        $w['status'] = array('eq',1);
        $list = Db::name('role')->field('id,role_name,role_auth_ids')->where($w)->select();
        $arr['data']=$list;
        $arr['meta']=array('msg'=>'获取用户列表成功','status'=>200);
        return json_encode($arr);

    }


    public function person()
    {
        $uid=session('uid');
        $v = Db::name('manager')->find($uid);

        if(Request::instance()->isPost()){
            echo $uid;



            print_r($_POST);
            $data['realname']=$_POST['realname'];
            $data['username']=$_POST['username'];
            $data['password']=md5($_POST['pass']);
            $oldpassword=md5($_POST['oldpass']);
            if($v['password']==$oldpassword){
                if(Db::name("manager")->where("id=".$uid)->update($data)){
                    $this->success("修改成功","/admin/manager/person.html");
                }else{
                    $this->error("修改失败");
                }
            }else{
                $this->error("原密码错误");
            }


        }
//        print_r($v);
        $this->assign("v",$v);


        return $this->fetch("person");
    }


    public function add()
    {
        $id=input('id',0);
        $data = input('post.');
        $db=Db::name("manager");
        unset($data['role_name']);
        $v=$db->find($id);
        //print_r($_POST);
        if($v){
        }else{
            $v['id']='';
            $v['realname']="";
            $v['username']="";
            $v['role_name']="";
            $v['role_id']="";
            $v['status']=1;
        }
        // return json_encode($data);die;
        if(Request::instance()->isPost()){

            $data['status']=$data['status']?1:0;
            if($id){//修改
                // print_r($data);
                $list = Db::name("manager")->where("id=".$id)->find();
                if($list['password'] == $data['password']){
                    unset($data['password']);
                }
                // dump($data);
                // die;
                if(Db::name("manager")->where("id=".$id)->update($data)){
                    $data['id'] = $id;
                    $arr['data']=$data;
                    $arr['meta']=array('msg'=>'修改用户成功','status'=>200);
                }else{
                    $arr['meta']=array('msg'=>'修改用户失败','status'=>300);
                }
            }else{//添加
                $data['password']=md5($data['password']);
                $data['sendtime']=time();
                $data['token']=$this->GenerateToken();
                if($id = Db::name("manager")->insert($data)){
                    $data['id'] = $id;
                    $arr['data']=$data;
                    $arr['meta']=array('msg'=>'添加用户成功','status'=>201);
                }else{
                    $arr['data']=null;
                    $arr['meta']=array('msg'=>'添加用户失败','status'=>300);
                }
            }

            return json_encode($arr);die;
        }
    }


    public function del()
    {
        $id=input('id',0);
        // dump($id);die();
        if(Db::name("manager")->delete($id)){
            $arr['data']=null;
            $arr['meta']=array('msg'=>'删除成功','status'=>200);
        }else{
            $arr['data']=null;
            $arr['meta']=array('msg'=>'删除失败','status'=>300);
        }

        return json_encode($arr);die;
    }

    public function audit()
    {

        $id=input("id",0);
        //echo $id;
        $v=Db::name("manager")->find($id);
        $data['status']=$v['status']?0:1;

        if(Db::name("manager")->where("id=".$id)->update($data)){
            $arr['data']=$v;
            $arr['meta']=array('msg'=>'获取用户列表成功','status'=>200);
        }else{
            $arr['data']=$v;
            $arr['meta']=array('msg'=>'获取用户列表失败','status'=>300);
        }

        echo json_encode($arr);
    }
}